package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.service.impl.CourseContentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {

    //展示对应课程的章节与课时信息
    public void findSectionAndLessonByCourseId(HttpServletRequest request, HttpServletResponse response){

        try {
            //获取参数
            int course_id = Integer.parseInt(request.getParameter("course_id"));

            //业务处理
            CourseContentService courseContentService = new CourseContentServiceImpl();
            List<Course_Section> list = courseContentService.findSectionAndLessonByCourseId(course_id);

            //返回结果
            String result = JSON.toJSONString(list);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据课程ID 回显课程信息
    public void findCourseById(HttpServletRequest request,HttpServletResponse response){

        try {
            //获取参数
            int course_id = Integer.parseInt(request.getParameter("course_id"));

            //业务处理
            CourseContentService contentService = new CourseContentServiceImpl();
            Course course = contentService.findCourseByCourseId(course_id);

            //返回JSON数据
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id","course_name");
            String result = JSON.toJSONString(course, filter);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //保存和修改章节信息
    public void saveOrUpdateSection(HttpServletRequest request, HttpServletResponse response){

        try {
            //获取参数
            Map<String,Object> map = (Map) request.getAttribute("map");
            //创建一个Course_section对象
            Course_Section section = new Course_Section();
            //使用BeanUtils工具类，将map中的数据封装到section
            //BeanUtils.populate(section,map);
            BeanUtils.copyProperties(section,map.get("section"));
            //业务处理
            CourseContentService courseContentService = new CourseContentServiceImpl();
            //如果没有携带ID信息则为保存章节，否则为修改
            if(section.getId() == 0){
                //调用新增章节信息方法
                String result = courseContentService.saveSection(section);
                response.getWriter().print(result);
            }else{
                //调用修改章节信息方法
                String result = courseContentService.updateSection(section);
                response.getWriter().print(result);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //修改章节状态
    public void updateSectionStatus(HttpServletRequest request, HttpServletResponse response){

        try {
            //接收参数
            int id = Integer.parseInt(request.getParameter("id"));
            int status = Integer.parseInt(request.getParameter("status"));

            //业务处理
            CourseContentService courseContentService = new CourseContentServiceImpl();
            String result = courseContentService.updateSectionStatus(id, status);

            //返回结果
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //保存&修改课时接口
    public void saveOrUpdateLesson(HttpServletRequest request, HttpServletResponse response){

        try {
            //获取参数
            Map<String,Object> map = (Map) request.getAttribute("map");
            //创建一个Course_lesson对象
            Course_Lesson lesson = new Course_Lesson();
            //使用BeanUtils工具类，将map中的数据封装到lesson
            //BeanUtils.populate(lesson,map);
            BeanUtils.copyProperties(lesson,map.get("lesson"));
            //业务处理
            CourseContentService courseContentService = new CourseContentServiceImpl();
            //如果没有携带ID信息则为保存课时信息，否则为修改课时
            if(lesson.getId() == 0){
                //调用新增课时信息方法
                String result = courseContentService.saveLesson(lesson);
                response.getWriter().print(result);
            }else{
                //调用修改课时信息方法
                String result = courseContentService.updateLesson(lesson);
                response.getWriter().print(result);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据章节ID 回显章节信息
    public void findSectionBySectionId(HttpServletRequest request,HttpServletResponse response){

        try {
            //获取参数
            int section_id = Integer.parseInt(request.getParameter("section_id"));

            //业务处理
            CourseContentService contentService = new CourseContentServiceImpl();
            Course_Section section = contentService.findSectionBySectionId(section_id);

            //返回JSON数据
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course_Section.class,"id","section_name");
            String result = JSON.toJSONString(section, filter);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
